package vivace.view.openDialogue;

import java.io.File;
import javax.swing.ImageIcon;

/**
 * A utility class for the other classes to use
 */
public class Utils {
	
	//declaration
    public final static String mid = "mid";

     /**
      * Get the extension of a file.
      * @param	File	file	The chosen file
      */
    public static String getExtension(File file) 
    {
    	
    	//Declaration of variables
        String extention = null;
        String fileName = file.getName();
        
        //Get the files last letters after a "."
        int indexFormat = fileName.lastIndexOf('.');

        //Find out if the last letters really are the last ones (the files format)
        if (indexFormat > 0 &&  indexFormat < fileName.length() - 1) {
        	
        	//if so, then save the to a variable in lowercase
        	//ex. song.mid -> mid
        	extention = fileName.substring(indexFormat+1).toLowerCase();
        }
        return extention;
    }

    /**
     * Returns a imageIcon
     * @param	String	path	the path to the icon
     * @return					an icon
     */
    protected static ImageIcon createImageIcon(String path) {
    	
    	//creates a path for the icon
        java.net.URL imgURL = Utils.class.getResource(path);
        
        //if there exists a icon with that path then return that one
        if (imgURL != null) {
            return new ImageIcon(imgURL);
            
        //else the icon could not be found
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}
